package com.naire.modul.dictype.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.common.dao.GeneralDaoImpl;
import net.common.util.BeanUtils;

import com.naire.modul.dictionary.pojo.Dictionary;
import com.naire.modul.dictionary.vo.DictionaryVo;
import com.naire.modul.dictype.dao.DictypeDao;
import com.naire.modul.dictype.pojo.Dictype;
import com.naire.modul.dictype.vo.DictypeVo;

public class DictypeDaoImpl extends GeneralDaoImpl implements DictypeDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<DictypeVo> search() throws Exception {
		StringBuilder hql = new StringBuilder("from Dictype ");
		List<DictypeVo> d = new ArrayList<DictypeVo>();
		List<Dictype> l = hibernateHandler.query(hql);
		for(Dictype bean : l) {
			DictypeVo dv = new DictypeVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
			Iterator<Dictionary> it = bean.getDictionarys().iterator();
			while(it.hasNext()) {
				DictionaryVo v = new DictionaryVo();
				BeanUtils.copyProperties(it.next(), v);
				dv.getVos().add(v);
			}
		}
		return d;
	}

	@Override
	public void insert(DictypeVo bean) throws Exception {
		Dictype pojo = new Dictype();
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(DictypeVo bean) throws Exception {
		Dictype pojo = (Dictype)hibernateHandler.find(Dictype.class, bean.getDict_sort_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(DictypeVo bean) throws Exception {
		update(bean);
	}

	@Override
	public DictypeVo find(Integer id) throws Exception {
		Dictype pojo = (Dictype)hibernateHandler.find(Dictype.class, id);
		DictypeVo bean = new DictypeVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}
}
